package wfmanage.action;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import edu.wfmanage.models.HelpDeskUser;
import edu.wfmanage.models.Ticket;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

public class ForwardToNextLevel extends ActionSupport{

	private int id;
	private String summary;
	private String category;
	private String device;
	private String os;
	private Date date;
	private String priority;
	private String cust_name;
	private String email;
	private String dept;
	private String office;
	private String phone;
	private String desc;
	private String comments;
	private int status;
	
	private String nextSupportLevel;
	private String commandbutton;
	private ArrayList<String> names;
	
	private ArrayList<HelpDeskUser> userlist = new ArrayList<HelpDeskUser>();
	
	public String execute(){
		
		if(this.commandbutton.startsWith("Forward")){
			ForwardToNextLevel.updateStatusbySupport(id, nextSupportLevel);
			userlist = HelpDeskUser.findUserList(nextSupportLevel);
			email mail=new email();
	        for(HelpDeskUser u: userlist){
	             MyLog.log("user is "+u.getEmailid());
	             mail.sendmail(u.getUsername(),u.getEmailid(),nextSupportLevel,0, null);
	             MyLog.log("An email has been sent to "+u.getEmailid());
	        }
			return "success";
		}
		
		return "initial_entry";
	}

		public static int updateStatusbySupport(int t_id, String support_levelname){
		
		MyLog.log("\nInside ForwardToNextLevel.updateStatusbySupport function...\n");
		String query1 = "select role_id from role where role_name= '" + support_levelname + "';";
		int support_id=0;
		Connection connection = DB.getConnection();
		ResultSet resultSet = DB.readFromDB(query1, connection);
		try {
			while (resultSet.next()) {
				support_id = resultSet.getInt("role_id");
			}
		}catch(SQLException ex){
			ex.printStackTrace();
		}
		int newStatus_id=0;
		String query2 = "select status_id from assignment where support_id = " + support_id + ";";
		resultSet = DB.readFromDB(query2, connection);
		try {
			while (resultSet.next()) {
				newStatus_id = resultSet.getInt("status_id");
			}
		}catch(SQLException ex){
			ex.printStackTrace();
		}
			
		String updateQuery = "update ticket set status_id = " + newStatus_id + " where ticket_id = " + t_id + " ;";
		
		return DB.update(updateQuery);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getDevice() {
		return device;
	}

	public void setDevice(String device) {
		this.device = device;
	}

	public String getOs() {
		return os;
	}

	public void setOs(String os) {
		this.os = os;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) {
		this.priority = priority;
	}

	public String getCust_name() {
		return cust_name;
	}

	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public String getOffice() {
		return office;
	}

	public void setOffice(String office) {
		this.office = office;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getNextSupportLevel() {
		return nextSupportLevel;
	}

	public void setNextSupportLevel(String nextSupportLevel) {
		this.nextSupportLevel = nextSupportLevel;
	}

	public String getCommandbutton() {
		return commandbutton;
	}

	public void setCommandbutton(String commandbutton) {
		this.commandbutton = commandbutton;
	}

	public ArrayList<String> getNames() {
		return names;
	}

	public void setNames(ArrayList<String> names) {
		this.names = names;
	}
	
	public ArrayList<HelpDeskUser> getUserlist() {
		return userlist;
	}

	public void setUserlist(ArrayList<HelpDeskUser> userlist) {
		this.userlist = userlist;
	}

	public static ArrayList<String> findStrings(String supportName){
		
		MyLog.log("\nInside ForwardToNextLevel.findStrings function...\n");
		ArrayList<String> usernames = new ArrayList<String>();
		ResultSet result = null;
		String query = "select user.user_name as name "
				+ "from user, user_role, role "
				+ "where user.user_id = user_role.user_id and "
				+ "user_role.role_id = role.role_id and "
				+ "role.role_name = '" + supportName + "';";
		Connection con = DB.getConnection();
		result = DB.readFromDB(query, con);
		
		try {
			while(result.next()){
				String name = result.getString("name");
				usernames.add(name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return usernames;
	}
}
